package com.yw.mq.example.m05_async;

import cn.hutool.core.util.IdUtil;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import static com.yw.mq.core.MqConstant.DEFAULT_PRODUCER_GROUP;
import static com.yw.mq.core.MqConstant.NAME_SERVER_ADDR;

public class Producer {
    public static void main(String[] args) throws Exception {

        //  创建生产者
        DefaultMQProducer producer = new DefaultMQProducer(DEFAULT_PRODUCER_GROUP);
        // 设置连接信息
        producer.setNamesrvAddr(NAME_SERVER_ADDR);
        // 启动
        producer.start();

        Message message = new Message();
        message.setKeys(IdUtil.getSnowflakeNextIdStr());
        message.setTopic("order_topic");
        message.setTags("food");
        message.setBody("hello mq".getBytes());

        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功：" + sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("发送失败：" + throwable.getMessage());
            }
        });

        System.out.println("发送完成");
        
        Thread.sleep(3000);

        producer.shutdown();

    }
}
